package mya.mes.mdc.model.tag.convert;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import mya.mes.mdc.model.excel.ExcelTagAlarmDef;
import mya.mes.mdc.model.tag.AlarmTag;

import javax.json.JsonArray;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AlarmTagConverter {

  public static void fromJson(Map<String, Object> json, AlarmTag obj) {
    for (java.util.Map.Entry<String, Object> member : json.entrySet()) {
      switch (member.getKey()) {
        case "excelTagAlarmRowList":
          if (member.getValue() instanceof List) {
            java.util.ArrayList<ExcelTagAlarmDef> list =  new java.util.ArrayList<>();
            ((List<Object>)member.getValue()).forEach( item -> {
              if (item instanceof Map){
                list.add(new ExcelTagAlarmDef((Map) item));
              }
            });
            obj.setExcelTagAlarmRowList(list);
          }
          break;
        case "mdcTag":
          if (member.getValue() instanceof String) {
            obj.setMdcTag((String)member.getValue());
          }
          break;
        case "msgNotice":
          if (member.getValue() instanceof Boolean) {
            obj.setMsgNotice((Boolean)member.getValue());
          }
          break;
        case "tagIndex":
          if (member.getValue() instanceof Number) {
            obj.setTagIndex(((Number)member.getValue()).intValue());
          }
          break;
        case "tagPrefix":
          if (member.getValue() instanceof String) {
            obj.setTagPrefix((String)member.getValue());
          }
          break;
        case "viewTag":
//          if (member.getValue() instanceof JsonObject) {
//            obj.setViewTag(new mya.michelin.mdc.model.view.ViewTag((JsonObject)member.getValue()));
//          }
          break;
      }
    }
  }

  public static void toJson(AlarmTag obj, java.util.Map<String, Object> json) {
    if (obj.getExcelTagAlarmRowList() != null) {
      JSONArray array = new JSONArray();
      obj.getExcelTagAlarmRowList().forEach(item -> array.add(JSONObject.toJSON(item)));
      json.put("excelTagAlarmRowList", array);
    }
    if (obj.getMdcTag() != null) {
      json.put("mdcTag", obj.getMdcTag());
    }
    if (obj.getMsgNotice() != null) {
      json.put("msgNotice", obj.getMsgNotice());
    }
    json.put("tagIndex", obj.getTagIndex());
    if (obj.getTagPrefix() != null) {
      json.put("tagPrefix", obj.getTagPrefix());
    }
  }
}
